/*
 * Copyright (c) 2021. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package cn.edu.ntu.yyl.service.imlp;

import cn.edu.ntu.yyl.dao.Article_commentDao;
import cn.edu.ntu.yyl.pojo.article.Article;
import cn.edu.ntu.yyl.pojo.article.ArticleComment;
import cn.edu.ntu.yyl.pojo.user.UserInfo;
import cn.edu.ntu.yyl.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private Article_commentDao Dao;
    //增
    public Integer InsertNewComment(ArticleComment articleComment) {
        //初始化

        try {
            //设置文章ID
            articleComment.setId(getmaxid());
            //执行SQL
            Dao.InsertNewComment(articleComment);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //删
    public Integer DeleteComment(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.DeleteComment(articleComment);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //改
    public Integer UpdateComment(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.UpdateComment(articleComment);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //查
    public List<ArticleComment> SelectAllComment() {
        //初始化
        try {
            return Dao.SelectAllComment();
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //按ID查询
    public ArticleComment SelectCommentById(Integer Id){
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCommentById(Id);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //审核评论
    public Integer CheckComment(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.CheckComment(articleComment);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //赞赏评论
    public Integer GivelikeComment(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.GivelikeComment(articleComment);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //按照状态查询
    public List<ArticleComment> SelectCommentByStatus(String status){
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCommentByStatus(status);
        } catch (Exception e) {
            return null;
        }
    }
    //置顶评论
    public Integer RecommendComment(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.RecommendComment(articleComment);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //查看文章的评论
    public List<ArticleComment> SelectCommentByArticle(Article article) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCommentByArticle(article);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //查看该评论的父评论
    public ArticleComment SelectFatherCommentByComment(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectFatherCommentByComment(articleComment);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //查看该评论的子评论
    public List<ArticleComment> SelectChildrenCommentByComment(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectChildrenCommentByComment(articleComment);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //查看某用户的评论
    public List<ArticleComment> SelectCommentByUser(UserInfo userInfo) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCommentByUser(userInfo);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //更新评论回复数量
    public Integer UpdateCommentReplyCount(ArticleComment articleComment) {
        //初始化

        try {
            //getMapper

            //执行SQL
            Dao.UpdateCommentReplyCount(articleComment);
        } catch (Exception e) {
            return -1;
        }
        return 1;
    }
    //按照ip获取评论
    public List<ArticleComment> SelectCommentByIP(String ip) {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectCommentByIP(ip);
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //时间线查询
    public List<ArticleComment> SelectTodayComment() {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectTodayComment();
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    public List<ArticleComment> SelectYesterdayComment() {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectYesterdayComment();
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    public List<ArticleComment> SelectSevenDayComment() {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectSevenDayComment();
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    public List<ArticleComment> SelectFourteenDayComment() {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectFourteenDayComment();
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    public List<ArticleComment> SelectMonthComment() {
        //初始化

        try {
            //getMapper

            //执行SQL
            return Dao.SelectMonthComment();
        } catch (Exception e) {
            System.err.println(e.toString());
            return null;
        }
    }
    //获取最大ID
    public Integer getmaxid() {
        //初始化
        List<ArticleComment> s = null;
        Integer id = -1;
        //取得SqlSession对象(openSession)

        try {
            //getMapper

            //获取列表
            s = Dao.SelectAllComment();
        } catch (Exception e) {
            System.err.println(e.toString());

        }
        //获取最大ID然后+1便是需要写入的userID
        if (s != null) {
            for (ArticleComment a : s) {
                if (id < a.getId()) {
                    id = a.getId();
                }
            }
        } else return -1;
        id++;
        return id;

    }
}
